package cn.com.utt.common.mapper;

import cn.com.utt.common.vo.Role;
import cn.com.utt.common.vo.User;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

@Mapper
public interface UserMapper {
	@Insert("insert into user(email,username,password,group_id,create_time,domain)values(#{email},#{username},#{password},#{groupId},now(),#{domain})")
	@Options(useGeneratedKeys=true, keyProperty="id", keyColumn="id")
	void save(User user);
	
	@Update("update user set email=#{email},username=#{username},password=#{password},group_id=#{groupId},lastlogin=#{lastlogin},lastfail=#{lastfail},failtimes=#{failtimes},sms_volume=#{smsVolume},last_reset_date=#{lastResetDate},domain=#{domain} where id=#{id}")
	void update(User user);
	
	@Select("SELECT r.* FROM role r,user_role ur where ur.rid=r.id and ur.uid=#{id}")
	List<Role> queryRolesById(@Param("id")long id);
	
	@Select("SELECT * FROM user where id=#{userId}")
	User queryById(@Param("userId")Long userId);
	
	@Select("SELECT * FROM user where group_id=#{groupId}")
	List<User> queryByGroupId(@Param("groupId")Long groupId);
	
	@Select({"<script>",
	    "SELECT username", 
	    "FROM user",
	    "WHERE ", 
	    "id IN", 
	      "<foreach item='item' index='index' collection='userIds'",
	        "open='(' separator=',' close=')'>",
	        "#{item}",
	      "</foreach>",
	"</script>"})
	List<String> queryUserNameByIds(@Param("userIds")Collection<Long> userIds);
	
	@Select("delete FROM user where id=#{userId}")
	void deleteById(@Param("userId")Long userId);
	
	@Delete({"<script>",
	    "delete ", 
	    "FROM user",
	    "WHERE ", 
	    "id IN", 
	      "<foreach item='item' index='index' collection='userIds'",
	        "open='(' separator=',' close=')'>",
	        "#{item}",
	      "</foreach>",
	"</script>"})
	void deleteByIds(@Param("userIds")List<Long>	userIds);
	
	@Select("SELECT * FROM user where parent_id=#{parentId}")
	List<User> queryByParentId(@Param("parentId")Long parentId);
	
	@Select({"<script>",
	    "SELECT id,group_id", 
	    "FROM user",
	    "WHERE ", 
	    "group_id IN", 
	      "<foreach item='item' index='index' collection='groupIds'",
	        "open='(' separator=',' close=')'>",
	        "#{item}",
	      "</foreach>",
	"</script>"})
	List<Map<String,Object>> queryUserIdsByGroupIds(@Param("groupIds")Collection<Long> groupIds);
	
	@Select({"<script>",
    "SELECT * ", 
    "FROM `user` ",
    "WHERE ", 
    "id in ", 
	  "<foreach item='item' index='index' collection='userIds'",
	  "open='(' separator=',' close=')'>",
	  "#{item}",
	"</foreach>",
      "<if test='searchs != null'>",
      "<foreach item='item' index='index' collection='searchs'",
        "open='and' separator='and' close=''>",
        "<choose>",
        "<when test='item.op != \"like\"'>",
        "${item.column}",
        " = ",
        "#{item.value}",
        "</when>",
        "<otherwise>",
        "${item.column}",
        " like ",
        "'%${item.value}%'",
        "</otherwise>",
        "</choose>",
      "</foreach>",
      "</if>",
      " limit #{start},#{rows}",
    "</script>"})
	List<User> queryByCondition(Map<String, Object> conditions);
	
	@Select({"<script>",
	    "SELECT COUNT(*) AS total", 
	    "FROM `user`",
	    "WHERE ", 
	    "id in ", 
	    	  "<foreach item='item' index='index' collection='userIds'",
	        "open='(' separator=',' close=')'>",
	        "#{item}",
	      "</foreach>",
	      "<if test='searchs != null'>",
	      "<foreach item='item' index='index' collection='searchs'",
	        "open='and' separator='and' close=''>",
	        "<choose>",
	        "<when test='item.op != \"like\"'>",
	        "${item.column}",
	        " = ",
	        "#{item.value}",
	        "</when>",
	        "<otherwise>",
	        "${item.column}",
	        " like ",
	        "'%${item.value}%'",
	        "</otherwise>",
	        "</choose>",
	      "</foreach>",
	      "</if>",
	    "</script>"})
	int countByCondition(Map<String, Object> conditions);
	
	@Select("select parent_id from `user` where id=#{userId}")
	Long queryParentIdById(@Param("userId") Long userId);
	
	@Select("SELECT id FROM `user` WHERE parent_id =#{parentId} or id =#{parentId}")
	List<Long> queryByParentIds(@Param("parentId")Long parentId);
	
	@Select("select id from `user` where parent_id=#{userId}")
	List<Long> queryIdsByParentId(@Param("userId") Long userId);
	
	@Select("SELECT * FROM user where username=#{value} or email=#{value}")
	User queryByUsernameOrEmail(@Param("value") String value);
	
}
